package org.elkastud.util;

import java.io.File;
import javax.swing.filechooser.FileFilter;

/**
 * FileFilter to open save project files
 * @author Mariusz Jasiński
 */
public class MFastaFileFilter extends FileFilter {

    private static final String M_FATSA = "inf";
    private static final String M_FATSA_DESCRIPTION = "*.inf";
    private static final String M_FATSA_OUT = "outf";
    private static final String M_FATSA_OUT_DESCRIPTION = "*.outf";

    private boolean format = false;

    /**
     * @param save if <code>true</code> save format else open format
     */
    public MFastaFileFilter(boolean save) {
        super();
        this.format = save;
    }

    public boolean accept(File pathname) {
        if (pathname.isDirectory()) {
            return true;
        }
        String extension = getExtension(pathname);
        if (!this.format) {
            if (M_FATSA.equals(extension)) {
                return true;
            }
            return false;
        } else {
            if (M_FATSA_OUT.equals(extension)) {
                return true;
            }
            return false;
        }
    }

    /**
     *
     * @param file file
     * @return file extension
     */
    private String getExtension(File file) {
        String str = file.getName();
        int index = str.lastIndexOf('.');
        if (index != -1 && index < str.length() - 1) {
            return str.substring(index + 1).toLowerCase();
        }
        return null;
    }

    @Override
    public String getDescription() {
        if (this.format) {
            return M_FATSA_OUT_DESCRIPTION;
        } else {
            return M_FATSA_DESCRIPTION;
        }
    }
}
